import click
from sayhello import app, db
from sayhello.models import Message

@app.cli.command()
@click.option("--drop", is_flag=True, help='Create after drop.')
def initdb(drop):
    "Initialize the database."

    if drop:
        click.confirm("This operation will delete the database, do you want to continue ?", abort=True)
        db.drop_all()
        click.echo("Drop tables")
    db.create_all()
    click.echo("Initialized the database.")


@app.cli.command()
@click.option("--count", default=20, help="Quantity of messages, default is 20.")
def forge(count):
    "Generate fake messages."
    from faker import Faker

    fake = Faker()

    db.drop_all()
    db.create_all()

    click.echo("Working...")
    for _ in range(count):
        message = Message(
            name = fake.name(),
            body = fake.sentence(),
            timestamp = fake.date_time_this_year()
        )

        db.session.add(message)

    db.session.commit()
    click.echo("Create %d fake messages." % count)